package com.xujl.widgetlibrary.util;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.xujl.quotelibrary.R;
import com.xujl.utilslibrary.view.ViewTool;


/**
 * Created by Administrator on 2016/10/10.
 */

public class ImageLoader {
    public static void loadImage (int res, final ImageView imageView) {
        if (res == 0) {
            imageView.setImageDrawable(null);
            return;
        }
        final ImageView.ScaleType scaleType = imageView.getScaleType();
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        final RequestOptions options = new RequestOptions();
        options.error(R.drawable.icon_error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.icon_default);//加载中显示的图片
        Glide.with(imageView.getContext()).load(res).apply(options).into(new DrawableImageViewTarget(imageView) {
            @Override
            public void onResourceReady (Drawable resource, @Nullable Transition<? super Drawable> transition) {
                //在这里添加一些图片加载完成的操作
                imageView.setImageDrawable(null);
                super.onResourceReady(resource, transition);
                imageView.setScaleType(scaleType);
            }
        });
    }

    public static void loadImage (String url, final ImageView imageView) {
        if (ViewTool.isEmpty(url)) {
            imageView.setImageDrawable(null);
            return;
        }
        final ImageView.ScaleType scaleType = imageView.getScaleType();
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        final RequestOptions options = new RequestOptions();
        options.error(R.drawable.icon_error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.icon_default);//加载中显示的图片
        Glide.with(imageView.getContext()).load(url).apply(options).into(new DrawableImageViewTarget(imageView) {
            @Override
            public void onResourceReady (Drawable resource, @Nullable Transition<? super Drawable> transition) {
                //在这里添加一些图片加载完成的操作
                imageView.setImageDrawable(null);
                super.onResourceReady(resource, transition);
                imageView.setScaleType(scaleType);
            }
        });
    }


}
